//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.purvar.boot.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.purvar.boot.api.entity.SysAutocodeDetail;
import com.purvar.boot.api.entity.SysAutocodeMain;
import com.purvar.boot.api.mapper.SysAutocodeMainMapper;
import com.purvar.boot.api.pojo.SysAutoCodeMainAndDetails;
import com.purvar.boot.api.pojo.SysAutocodeMainEditVo;
import com.purvar.boot.api.pojo.SysAutocodeMainNewVo;
import com.purvar.boot.api.pojo.SysAutocodeMainQueryVo;
import com.purvar.boot.api.service.IMySQLGeneratorService;
import com.purvar.boot.api.service.ISysAutocodeDetailService;
import com.purvar.boot.api.service.ISysAutocodeMainService;
import com.purvar.boot.comm.api.vo.BaseEntity;
import com.purvar.boot.comm.api.vo.ColumnEntityExtend;
import com.purvar.boot.comm.api.vo.Result;
import com.purvar.boot.comm.api.vo.TableEntity;
import com.purvar.boot.comm.core.query.QueryGenerator;
import com.purvar.boot.comm.exception.BusinessException;
import com.purvar.boot.comm.util.AutoCodeUtils;
import com.purvar.boot.comm.util.GenUtils;
import com.purvar.boot.comm.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysAutocodeMainServiceImpl extends ServiceImpl<SysAutocodeMainMapper, SysAutocodeMain> implements ISysAutocodeMainService {
    private static final Logger log = LoggerFactory.getLogger(SysAutocodeMainServiceImpl.class);
    @Autowired
    private IMySQLGeneratorService mySQLGeneratorService;
    @Autowired
    private ISysAutocodeDetailService sysAutocodeDetailService;

    public SysAutocodeMainServiceImpl() {
    }

    @Transactional(
            rollbackFor = {Exception.class}
    )
    public void addSysAutocodeMain(SysAutocodeMainNewVo sysAutocodeMainNewVo) {
        SysAutocodeMain sysAutocodeMain = new SysAutocodeMain();
        BeanUtils.copyProperties(sysAutocodeMainNewVo, sysAutocodeMain);
        LambdaQueryWrapper<SysAutocodeMain> query = new LambdaQueryWrapper();
        query.eq(BaseEntity::getDelFlag, "1");
        SysAutocodeMain oneSysAutocodeMain = (SysAutocodeMain) this.getOne(query);
        if (null == oneSysAutocodeMain) {
            sysAutocodeMain.setDelFlag("1");
            ((SysAutocodeMainMapper) this.baseMapper).insert(sysAutocodeMain);
        } else {
            throw new BusinessException("", new Object[0]);
        }
    }

    @Transactional(
            rollbackFor = {Exception.class}
    )
    public void updateSysAutocodeMain(String id, SysAutocodeMainEditVo sysAutocodeMainEditVo) {
        SysAutocodeMain sysAutocodeMain = new SysAutocodeMain();
        sysAutocodeMain.setId(id);
        BeanUtils.copyProperties(sysAutocodeMainEditVo, sysAutocodeMain);
        ((SysAutocodeMainMapper) this.baseMapper).updateById(sysAutocodeMain);
    }

    public Result<IPage<SysAutocodeMain>> listPage(SysAutocodeMainQueryVo sysAutocodeMainQueryVo, Integer pageNo, Integer pageSize, HttpServletRequest req) {
        Result<IPage<SysAutocodeMain>> result = new Result();
        SysAutocodeMain sysAutocodeMain = new SysAutocodeMain();
        BeanUtils.copyProperties(sysAutocodeMainQueryVo, sysAutocodeMain);
        QueryWrapper<SysAutocodeMain> queryWrapper = QueryGenerator.initQueryWrapper(sysAutocodeMain, req.getParameterMap());
        Page<SysAutocodeMain> page = new Page((long) pageNo, (long) pageSize);
        IPage<SysAutocodeMain> pageList = this.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    public SysAutoCodeMainAndDetails getSysAutoCodeMainAndDetailsBytableName(String tableName) {
        if (tableName != null) {
            TableEntity tableEntity = this.mySQLGeneratorService.getdataBaseTableEntity(tableName);
            SysAutoCodeMainAndDetails sysAutoCodeMainAndDetails = new SysAutoCodeMainAndDetails();
            SysAutocodeMain sysAutocodeMain = new SysAutocodeMain();
            sysAutocodeMain.setAutoTableName(tableEntity.getTableName());
            sysAutocodeMain.setAutoClassName(tableEntity.getClassName());
            sysAutocodeMain.setSystype(tableEntity.getClassname());
            sysAutocodeMain.setFunctionDescribe(tableEntity.getComments());
            sysAutocodeMain.setAutoPackPath("com.purvar.boot");
            sysAutocodeMain.setMoudleName(tableEntity.getClassname());
            sysAutoCodeMainAndDetails.setSysAutocodeMain(sysAutocodeMain);
            int ins = 1;
            Iterator var6 = tableEntity.getColumns().iterator();

            while (var6.hasNext()) {
                ColumnEntityExtend columnEntityExtend = (ColumnEntityExtend) var6.next();
                SysAutocodeDetail sysAutocodeDetail = new SysAutocodeDetail();
                sysAutocodeDetail.setDatabaseName(columnEntityExtend.getColumnName());
                sysAutocodeDetail.setDatabaseType(columnEntityExtend.getDataType());
                sysAutocodeDetail.setDatabaseDescription(columnEntityExtend.getComments());
                sysAutocodeDetail.setJavaName(columnEntityExtend.getAttrName());
                sysAutocodeDetail.setJavaType(columnEntityExtend.getAttrType());
                if (StringUtils.isNotEmpty(columnEntityExtend.getMainId())) {
                    sysAutocodeDetail.setMainkey("1");
                }

                if (!GenUtils.isBasicFiled(columnEntityExtend.getColumnName())) {
                    sysAutocodeDetail.setEdit("0");
                } else {
                    sysAutocodeDetail.setEdit("1");
                }

                sysAutocodeDetail.setOrderIndex(ins + "");
                sysAutocodeDetail.setMaximumLength(columnEntityExtend.getMaximumLength());
                ++ins;
                sysAutoCodeMainAndDetails.getSysAutocodeDetails().add(sysAutocodeDetail);
            }

            return sysAutoCodeMainAndDetails;
        } else {
            return null;
        }
    }

    public SysAutoCodeMainAndDetails getEditSysAutoCodeMainAndDetailsBytableName(String tableName) {
        SysAutocodeMain sysAutocodeMainQuery = new SysAutocodeMain();
        sysAutocodeMainQuery.setAutoTableName(tableName);
        QueryWrapper queryWrapper = new QueryWrapper(sysAutocodeMainQuery);
        SysAutoCodeMainAndDetails sysAutoCodeMainAndDetails = new SysAutoCodeMainAndDetails();
        List<SysAutocodeMain> sysAutocodeMains = ((SysAutocodeMainMapper) this.baseMapper).selectList(queryWrapper);
        if (sysAutocodeMains.size() > 0) {
            SysAutocodeMain sysAutocodeMain = (SysAutocodeMain) sysAutocodeMains.get(0);
            sysAutoCodeMainAndDetails.setSysAutocodeMain(sysAutocodeMain);
            SysAutocodeDetail sysAutocodeDetail = new SysAutocodeDetail();
            sysAutocodeDetail.setMainId(sysAutocodeMain.getId());
            QueryWrapper queryWrapper2 = new QueryWrapper(sysAutocodeDetail);
            queryWrapper2.orderByAsc("order_index+0");
            sysAutoCodeMainAndDetails.setSysAutocodeDetails(this.sysAutocodeDetailService.getBaseMapper().selectList(queryWrapper2));
        }

        return sysAutoCodeMainAndDetails;
    }

    public void addSysAutoCodeMainAndDetails(SysAutoCodeMainAndDetails entity) {
        entity.getSysAutocodeMain().setDelFlag("1");
        this.saveOrUpdate(entity.getSysAutocodeMain());
        Iterator var2 = entity.getSysAutocodeDetails().iterator();

        while (var2.hasNext()) {
            SysAutocodeDetail sysAutocodeDetail = (SysAutocodeDetail) var2.next();
            sysAutocodeDetail.setMainId(entity.getSysAutocodeMain().getId());
            sysAutocodeDetail.setDelFlag("1");
        }

        this.sysAutocodeDetailService.saveOrUpdateBatch(entity.getSysAutocodeDetails());
    }

    public byte[] autocodeMain(String id) {
        SysAutocodeMain sysAutocodeMain = (SysAutocodeMain) ((SysAutocodeMainMapper) this.baseMapper).selectById(id);
        SysAutocodeDetail sysAutocodeDetail = new SysAutocodeDetail();
        sysAutocodeDetail.setMainId(sysAutocodeMain.getId());
        QueryWrapper<SysAutocodeDetail> sysAutocodeDetailQueryWrapper = new QueryWrapper(sysAutocodeDetail);
        sysAutocodeDetailQueryWrapper.orderByAsc("order_index");
        List<SysAutocodeDetail> sysAutocodeDetails = this.sysAutocodeDetailService.list(sysAutocodeDetailQueryWrapper);
        //处理排序
        List<SysAutocodeDetail> SysAutocodeDetails = sysAutocodeDetails.stream().sorted(Comparator.comparing(SysAutocodeDetail::getOrderIndexs)).collect(Collectors.toList());
        return AutoCodeUtils.genCode(sysAutocodeMain, SysAutocodeDetails);
    }
}
